package com.yzd.crm.dao.impl;

import com.yzd.crm.dao.LinkManDao;
import com.yzd.crm.domain.LinkMan;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projection;
import org.hibernate.criterion.Projections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate5.HibernateTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @Auther: Ye Zhengdong
 * @Date: 2019/4/5 17:37
 * @Description:
 */
@Repository("linkManDao")
public class LinkManDaoImpl implements LinkManDao {

    @Autowired
    private HibernateTemplate hibernateTemplate;

    @Override
    public void save(LinkMan linkMan) {
        hibernateTemplate.save(linkMan);
    }

    @Override
    public List<LinkMan> findAll() {
        return (List<LinkMan>) hibernateTemplate.find("from LinkMan");
    }

    @Override
    public List<LinkMan> findByDc(DetachedCriteria dc) {
        return (List<LinkMan>) hibernateTemplate.findByCriteria(dc);
    }

    @Override
    public LinkMan findById(Integer lkmId) {
        return hibernateTemplate.get(LinkMan.class, lkmId);
    }

    @Override
    public void update(LinkMan linkMan) {
        hibernateTemplate.update(linkMan);
    }

    @Override
    public void delete(LinkMan linkMan) {
        hibernateTemplate.delete(linkMan);
    }

    @Override
    public int findCount(DetachedCriteria dc) {
        dc.setProjection(Projections.rowCount());
        List<Long> count = (List<Long>) hibernateTemplate.findByCriteria(dc);
        return count.get(0).intValue();
    }

    @Override
    public List<LinkMan> findPage(DetachedCriteria dc, int start, Integer pageSize) {
        dc.setProjection(null);
        return (List<LinkMan>) hibernateTemplate.findByCriteria(dc, start, pageSize);
    }
}
